package com.lin.missyou.api.v1;

import com.lin.missyou.exception.http.NotFoundException;
import com.lin.missyou.model.Category;
import com.lin.missyou.model.GridCategory;
import com.lin.missyou.service.CategoryService;
import com.lin.missyou.service.GridCategoryService;
import com.lin.missyou.vo.CategoryAllVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 *
 * 一次查询出所有数据，根据数据量确定
 * 返回数据格式定义，方便前端用户
 *      与前端协商
 * @author gcq
 * @Create 2021-10-24
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private GridCategoryService gridCategoryService;
    /**
     * 放到Vo里面转换，简洁，复用
     * @return
     */
    @GetMapping("/all")
    public CategoryAllVO getAll()
    {
        Map<Integer, List<Category>> categories = categoryService.getAll();
        return new CategoryAllVO(categories);
    }

    @GetMapping("/grid/all")
    public List<GridCategory> getGridCategoryList()
    {
        List<GridCategory> gridCategoryList = gridCategoryService.getGridCategoryList();
        if(gridCategoryList.isEmpty())
        {
            throw new NotFoundException(30009);
        }
        return gridCategoryList;

    }
}